package com.example.cd5160866.urlapp.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.os.Build;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.TranslateAnimation;

/**
 * Created by cd5160866 on 16/7/28.
 */
public class AnimotionUtils {
    public static void showViewWithAnimotion(View view, int offsetX, int offsetY) {
        view.setVisibility(View.VISIBLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Animator animator = ViewAnimationUtils.createCircularReveal(view, ViewUtils.getScreenWidth(view.getContext()) - offsetX, offsetY, 0, (ViewUtils.getScreenWidth(view.getContext())) * 1.2f);
            animator.setInterpolator(new AccelerateDecelerateInterpolator());
            animator.setDuration(600).start();
        } else {
            TranslateAnimation animation = new TranslateAnimation(ViewUtils.getScreenWidth(view.getContext()), 0, 0, 0);
            animation.setDuration(300);
            view.setAnimation(animation);
        }
    }

    public static void hideViewWithAnimotion(final View view, int offsetX, int offsetY) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Animator animator = ViewAnimationUtils.createCircularReveal(view, ViewUtils.getScreenWidth(view.getContext()) - offsetX, offsetY, (ViewUtils.getScreenWidth(view.getContext())) * 1.2f, 0);
            animator.setInterpolator(new AccelerateDecelerateInterpolator());
            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    view.setVisibility(View.GONE);
                }
            });
            animator.setDuration(600).start();
        } else {
            TranslateAnimation animation = new TranslateAnimation(0, ViewUtils.getScreenWidth(view.getContext()), 0, 0);
            animation.setDuration(300);
            view.setAnimation(animation);
            view.setVisibility(View.GONE);
        }
    }
}
